home *** CD-ROM | disk | FTP | other *** search
/ C & C++ Multimedia Cyber Classroom / C and C++ Multimedia Cyber Classroom (Prentice Hall) (1998).iso / src / fig02_07.jar / Ch02 / Fig02_07 / Fig02_07.cpp
C/C++ Source or Header  |  1997-10-11  |  976b  |  31 lines

  1. // Fig. 2.7: fig02_07.cpp
  2. // Class average program with counter-controlled repetition
  3. #include <iostream.h>
  4.  
  5. int main()
  6. {
  7.    int total,        // sum of grades 
  8.        gradeCounter, // number of grades entered
  9.        grade,        // one grade
  10.        average;      // average of grades
  11.  
  12.    // initialization phase
  13.    total = 0;                           // clear total
  14.    gradeCounter = 1;                    // prepare to loop
  15.  
  16.    // processing phase
  17.    while ( gradeCounter <= 10 ) {       // loop 10 times
  18.       cout << "Enter grade: ";          // prompt for input
  19.       cin >> grade;                     // input grade
  20.       total = total + grade;            // add grade to total
  21.       gradeCounter = gradeCounter + 1;  // increment counter
  22.    }
  23.  
  24.    // termination phase
  25.    average = total / 10;                // integer division
  26.    cout << "Class average is " << average << endl;
  27.  
  28.    return 0;   // indicate program ended successfully
  29. }
  30.  
  31.